<template>
  <!-- 商品展示组件 -->
  <div class="goods">
    <div class="gods_left" ref="menuWrapper">
      <ul>
        <li
          v-for="(item,index) in goods"
          :key="index"
          class="menu-item"
          :class="{'current':currentIndex==index}"
          @click="selectMenu(index,$event)"
        >
          <span class="text">
            <span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>
            {{item.name}}
          </span>
        </li>
      </ul>
    </div>
    <div class="gods_right" ref="foodsWrapper">
      <ul>
        <li class="food-list food-list-hook" v-for="(item,value) in goods" :key="value">
          <h1 class="title">{{item.name}}</h1>
          <ul>
            <li
              v-for="(food,index) in item.foods"
              :key="index"
              class="food-item"
              @click="selectFood(food,$event)"
            >
              <div class="icon">
                <img width="57" :src="food.icon">
              </div>
              <div class="content">
                <h2 class="name">{{food.name}}</h2>
                <p class="desc">{{food.description}}</p>
                <div class="sale">
                  <span class="count">月售{{food.sellCount}}份</span>
                  <span>好评率{{food.rating}}</span>
                </div>
                <div class="price">
                  <span class="now">¥{{food.price}}</span>
                  <span class="old" v-if="food.oldPrice">¥{{food.oldPrice}}</span>
                </div>
                <div class="cartcon-wrapper">
                  <cartcontrot :food="food"></cartcontrot>
                </div>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <!-- 底部订单量 -->
    <shorpcart
      :selectFoods="selectFoods"
      :delivery-price="seller.deliveryPrice"
      :min-price="seller.minPrice"
    ></shorpcart>
    <!-- 商品详情 -->
    <food 
    :food="selectedFood"
    ref="food"></food>
  </div>
</template>

<script>
import BScroll from "better-scroll";
import shorpcart from "../shorpcart/shorpcart";
import cartcontrot from "../cartcontrot/cartcontrot";
import food from "../food/food";

export default {
  components: {
    shorpcart,
    cartcontrot,
    food
  },
  props: {
    seller: {
      type: Object
    }
  },
  data() {
    return {
      goods: [],
      listHeight: [],
      scrollY: 0,
      selectedFood: {}
    };
  },
  computed: {
    currentIndex() {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i];
        let height2 = this.listHeight[i + 1];
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          return i;
        }
      }
      return 0;
    },
    selectFoods() {
      let foods = [];
      this.goods.forEach(good => {
        good.foods.forEach(food => {
          if (food.count) {
            foods.push(food);
          }
        });
      });
      return foods;
    }
  },
  created() {
    this.axios.get("http://localhost:3000/api/goods").then(res => {
      this.goods = res.data.goods;
      this.$nextTick(() => {
        this._initScroll(); //初始化滚动函数
        this._calculateHeight();
      });
    }),
      (this.classMap = ["jian", "jian1", "jian2", "jian3", "jian4"]);
  },
  methods: {
    selectMenu(index, event) {
      if (!event._constructed) {
        //解决在pc端点击会点击两次
        return;
      }
      let foodList = this.$refs.foodsWrapper.getElementsByClassName(
        "food-list-hook"
      ); //先获取到所有的li
      let el = foodList[index]; //再获取到下标
      this.foodsScroll.scrollToElement(el, 300); //到对应下标,300毫秒
    },
    _initScroll() {
      this.meunScroll = new BScroll(this.$refs.menuWrapper, {
        click: true //默认点击事件被阻止了,所以在这里设置设click为true
      });
      this.foodsScroll = new BScroll(this.$refs.foodsWrapper, {
        click: true,
        probeType: 3
      });

      this.foodsScroll.on("scroll", pos => {
        this.scrollY = Math.abs(Math.round(pos.y));
      });
    },
    _calculateHeight() {
      let foodList = this.$refs.foodsWrapper.getElementsByClassName(
        "food-list-hook"
      );
      let height = 0;
      this.listHeight.push(height);
      for (let i = 0; i < foodList.length; i++) {
        let item = foodList[i];
        height += item.clientHeight;
        this.listHeight.push(height);
      }
    },
    selectFood(food, event) {
      if (!event._constructed) {
        //解决在pc端点击会点击两次
        return;
      }
      this.selectedFood = food;
      this.$refs.food.show();//调用子组件的这个方法
    }
  }
};
</script>

<style lang = "scss">
.goods {
  width: 100%;
  position: absolute;
  top: 176px;
  bottom: 46px;
  overflow: hidden;
  display: flex;
  .gods_left {
    width: 80px;
    background: #f3f5f7;
    flex: 0 0 80px;
    .menu-item {
      display: table;
      height: 54px;
      width: 56px;
      line-height: 14px;
      padding: 0 12px;
    }
    .jian {
      display: inline-block;
      width: 15px;
      height: 15px;
      background: url("decrease_3@2x.png") no-repeat 0 0 / cover;
      margin-right: 6px;
    }
    .jian1 {
      display: inline-block;
      width: 12px;
      height: 12px;
      background: url("discount_3@2x.png") no-repeat 0 0 / cover;
      margin-right: 2px;
    }
    .jian2 {
      display: inline-block;
      width: 12px;
      height: 12px;
      background: url("guarantee_3@2x.png") no-repeat 0 0 / cover;
      margin-right: 2px;
    }
    .jian3 {
      display: inline-block;
      width: 12px;
      height: 12px;
      background: url("invoice_3@2x.png") no-repeat 0 0 / cover;
      margin-right: 2px;
    }
    .jian4 {
      display: inline-block;
      width: 12px;
      height: 12px;
      background: url("special_3@2x.png") no-repeat 0 0 / cover;
      margin-right: 2px;
    }
    .text {
      display: table-cell;
      width: 56px;
      vertical-align: middle;
      font-size: 12px;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);
    }
  }
  .current {
    position: relative;
    z-index: 10;
    margin-top: -1px;
    background: #fff;
    font-weight: 700;
    .text {
      border: none;
    }
  }
  .gods_right {
    flex: 1;
    .title {
      padding-left: 14px;
      height: 26px;
      line-height: 26px;
      border-left: 2px solid #d9dde1;
      color: rgb(147, 153, 159);
      background: #f3f5f7;
    }
    .food-item {
      position: relative;
      display: flex;
      margin: 18px;
      padding-bottom: 18px;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);

      .icon {
        flex: 0 0 57px;
        margin-right: 10px;
      }
      .content {
        flex: 1;
        .name {
          margin: 2px 0 8px 0;
          font-size: 14px;
          color: rgb(7, 17, 27);
          line-height: 14px;
        }

        .desc,
        .sale {
          font-size: 10px;
          color: rgb(147, 153, 159);
          line-height: 14px;
        }
        .desc {
          margin-bottom: 8px;
        }
        .sale {
          .count {
            margin-right: 12px;
          }
        }
        .price {
          font-weight: 700;
          line-height: 24px;
          .now {
            margin-right: 8px;
            font-size: 14px;
            color: rgb(240, 20, 20);
          }
          .old {
            text-decoration: line-through;
            font-size: 10px;
            color: rgb(147, 153, 159);
          }
        }
        .cartcon-wrapper {
          position: absolute;
          right: 0;
          bottom: 12px;
        }
      }
    }
    .food-item:last-child {
      border-bottom: 1px solid transparent;
      margin-bottom: 0;
    }
  }
}
</style>
